package com.wwmz.pattern.singleton.lazy;

/**
 * 懒汉式单例模式在外部需要使用的时候才进行实例化
 * Created by wumz on 2020/9/16 16:48
 */
public class LazyDoubleCheckSingleton {

    private LazyDoubleCheckSingleton() {
    }
    //静态块，公共内存区域
    private volatile static LazyDoubleCheckSingleton instance = null;

    public static LazyDoubleCheckSingleton getInstance(){
        //检查是否要阻塞
        if (null == instance){
            synchronized (LazyDoubleCheckSingleton.class){
                //检查是否要重新创建实例
                if (null == instance){
                    instance = new LazyDoubleCheckSingleton();
                }
            }
        }
        return instance;
    }
}
